package com.market.cash.ui.fragment

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.market.cash.R
import com.market.cash.databinding.CashDataSafetyBinding
import com.market.cash.pro.ProFragment
import com.market.cash.ui.activity.KefuActivity
import com.market.cash.utils.event.CashRootEventType
import com.market.cash.utils.helper.CashRootCustomConfigsHelper


/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/18 10:17
 * @Annotation：隐私协议
 */
class DataSafetyFragment : ProFragment() {

    private lateinit var binding: CashDataSafetyBinding

    override fun getCashRootRootView(inflater: LayoutInflater): View {
        binding = CashDataSafetyBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initCashRootView() {
        setCashRootTitleBarVisibility(View.VISIBLE)
        setCashRootTitleBarColor(Color.parseColor("#FFFFFFFF"))
        setCashRootBackBtnVisibility(View.INVISIBLE)
        setCashRootTextTitle(getString(R.string.privacy_policy_title))
        setCashRootTextTitleGravity(Gravity.CENTER)
        setCashRootRightBtnRes(R.mipmap.cash_kefu_black)

        initWebViewSettings()
    }

    override fun initCashRootListener() {
        //联系客服
        rootViewBinding.imgKefu.setOnClickListener {
            KefuActivity.start(requireContext(),"Data Safety Page")
            addCashRootEventPoint(CashRootEventType.CLICK.CASHROOT_CLICK_CUSTOMER,"Data Safety Page")
        }
    }

    override fun initCashRootData() {
        val linkUrl = CashRootCustomConfigsHelper.getCashRootPrivacyPolicyLink()
        binding.ppWebView.loadUrl(linkUrl)
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebViewSettings() {
        binding.ppWebView.setInitialScale(100)
        binding.ppWebView.webChromeClient = mWebChromeClient
        binding.ppWebView.webViewClient = mWebClient
        val webSettings = binding.ppWebView.settings
        webSettings.setSupportZoom(true)
        try {
            webSettings.javaScriptEnabled = true
            webSettings.textZoom = 100
            webSettings.domStorageEnabled = true
            webSettings.builtInZoomControls = true
            webSettings.displayZoomControls = true
            webSettings.useWideViewPort = true
            webSettings.loadWithOverviewMode = true
            webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
            webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
            webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            webSettings.allowUniversalAccessFromFileURLs = true
            webSettings.allowFileAccessFromFileURLs = true
            webSettings.allowContentAccess = true
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private val mWebChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)

            binding.progressBar.progress = newProgress
            if (newProgress == 100) {
                binding.progressBar.visibility = View.GONE
            } else {
                binding.progressBar.visibility = View.VISIBLE
            }
        }
    }

    private val mWebClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?,
        ): Boolean {
            return shouldOverrideUrlLoading(view, request!!.url.toString())
        }

        @Deprecated("Deprecated in Java", ReplaceWith("false"))
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            return false
        }
    }

    override fun onStart() {
        super.onStart()
        addCashRootEventPoint(CashRootEventType.PAGE.CASHROOT_PAGE_DATA_SAFETY_START,"Data Safety Page")
    }

    override fun onDestroy() {
        super.onDestroy()
        addCashRootEventPoint(CashRootEventType.PAGE.CASHROOT_PAGE_DATA_SAFETY_END,"Data Safety Page")
    }
}